package com.youlai.boot.modules.merchant.service;

import com.youlai.boot.modules.merchant.model.MerchantForm;
import com.youlai.boot.modules.merchant.model.MerchantVO;
import com.youlai.boot.modules.merchant.model.Merchant;
import com.youlai.boot.modules.merchant.model.MerchantPageQuery;
import com.youlai.boot.modules.merchant.mapper.MerchantMapper;
import com.youlai.boot.modules.merchant.model.MerchantConverter;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import com.youlai.boot.modules.brand.model.BrandVO;
import com.youlai.boot.modules.brand.model.BrandPageQuery;
import com.youlai.boot.modules.brand.mapper.BrandMapper;

@Service
@RequiredArgsConstructor
public class MerchantService extends ServiceImpl<MerchantMapper, Merchant> {

    private final MerchantMapper merchantMapper;

    private final MerchantConverter merchantConverter;
    private final BrandMapper brandMapper;

    public IPage<MerchantVO> getMerchantPage(MerchantPageQuery query) {
        Page<MerchantVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return merchantMapper.getMerchantPage(page, query);
    }

    public boolean save(MerchantForm form) {
        Merchant merchant = merchantConverter.toEntity(form);
        return merchantMapper.insert(merchant) > 0;
    }

    public boolean update(Long id, MerchantForm form) {
        Merchant merchant = merchantConverter.toEntity(form);
        merchant.setId(id);
        return merchantMapper.updateById(merchant) > 0;
    }

    public MerchantForm getForm(Long id) {
        Merchant merchant = merchantMapper.selectById(id);
        return merchantConverter.toForm(merchant);
    }

    /**
     * 通过品牌ID查询关联的商户分页列表
     * 
     * @param brandId 品牌ID
     * @param query   分页查询参数
     * @return 商户分页数据
     */
    public IPage<MerchantVO> getMerchantPageByBrandId(Long brandId, MerchantPageQuery query) {
        Page<MerchantVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return merchantMapper.getMerchantPageByBrandId(page, brandId, query);
    }
}
